#!/bin/bash
# Copyright (c) Huawei Technologies Co., Ltd. 2010-2018. All rights reserved.
# description: the script that make install libcurl
# date: 2020-11-16
# version: 3.19.9.3
# history:
# 2020-04-11 upgrade to 3.19.9.3
set -e

TAR_FILE=${PWD}/huaweicloud-sdk-c-obs-3.19.9.3.tar.gz
SRC_DIR=${PWD}/huaweicloud-sdk-c-obs
ARCH=`uname -m`

TRUNK_DIR=${PWD}/../../
THIRD_PARTY_BINARYLIBS=$THIRDPARTY_FOLDER

INSTALL_DIR="$TRUNK_DIR/output/kernel/dependency/libobs/comm/"
PREFIX_DIR="$TRUNK_DIR/dependency/esdk_obs_api"
LOG_FILE="$TRUNK_DIR/dependency/esdk_obs_api/libobs.log"


#######################################################################
#  Print log.
#######################################################################
log()
{
    printf "[Build libobs] $(date +%y-%m-%d): $@"
    echo "[Build libobs] $(date +%y-%m-%d): $@" >> "$LOG_FILE" 2>&1
}

print_done()
{
    echo "done"
}

#######################################################################
#  print log and exit.
#######################################################################
die()
{
    log "$@"
    echo "$@"
    echo "For more information, please see the log file: $LOG_FILE"
    exit $BUILD_FAILED
}

checkret()
{
    if [ $? -ne 0 ]
    then
        die "[Error] " $1
    fi
}

main()
{
    if [ -d ${SRC_DIR} ]; then
        rm -rf ${SRC_DIR}
    fi
    mkdir $SRC_DIR
    tar -zxf $TAR_FILE -C $SRC_DIR --strip-components 1

    rm -f $LOG_FILE;

    cd ${SRC_DIR}
    log "[Info] patching ......... "
    patch -p1 < ../obs.patch >> $LOG_FILE 2>&1
    if [[ "$ARCH"x = "loongarch64"x ]];then
	    cp -rf ${TRUNK_DIR}/build-aux/* ./platform/eSDK_LogAPI_V2.1.10/log4cpp/config/
    fi

    cd ..
    cp Makefile huaweicloud-sdk-c-obs/platform/eSDK_LogAPI_V2.1.10/eSDKLogAPI/

    source ${PREFIX_DIR}/build_logAPI.sh
    source ${PREFIX_DIR}/build_obs.sh

    build_logAPI
    build_obs

    cd ${SRC_DIR}
    
    log "[Info] Successfully installed libobs into $INSTALL_DIR"
    echo "copying lib..."
    mkdir -p ${INSTALL_DIR}/lib
    mkdir -p ${INSTALL_DIR}/include
    # copy libeSDKLogAPI to libobs
    if [ "$ARCH" = "aarch64" ];then
        cp ${SRC_DIR}/platform/eSDK_LogAPI_V2.1.10/C/aarch64/libeSDKLogAPI.so ${INSTALL_DIR}/lib/
    else
        cp ${SRC_DIR}/platform/eSDK_LogAPI_V2.1.10/C/linux_64/libeSDKLogAPI.so ${INSTALL_DIR}/lib/
    fi
    cp -d ${SRC_DIR}/platform/eSDK_LogAPI_V2.1.10/log4cpp/build/lib/liblog4cpp.so* ${INSTALL_DIR}/lib/
    # copy pcre lib to libobs
    cp -d ${TRUNK_DIR}/dependency/pcre/install_comm/lib/libpcre.so* ${INSTALL_DIR}/lib/
    # copy libiconv lib to libobs
    cp -d ${TRUNK_DIR}/dependency/libiconv/install_comm/lib/libcharset.so* ${INSTALL_DIR}/lib/
    cp -d ${TRUNK_DIR}/dependency/libiconv/install_comm/lib/libiconv.so* ${INSTALL_DIR}/lib/
    # copy nghttp2 lib to libobs
    cp -d ${TRUNK_DIR}/dependency/nghttp2/install_comm/lib/libnghttp2.so* ${INSTALL_DIR}/lib/
    # copy libxml2 lib to libobs
    cp -d ${TRUNK_DIR}/dependency/libxml2/install_comm/lib/libxml2.so* ${INSTALL_DIR}/lib/
    # copy libobs to install dir
    cd ${SRC_DIR}/source/eSDK_OBS_API/eSDK_OBS_API_C++
    cp OBS.ini ${INSTALL_DIR}/lib/
    cd ./build
    cp ./include/eSDKOBS.h ${INSTALL_DIR}/include/
    cp ./lib/libeSDKOBS.so ${INSTALL_DIR}/lib/
}


main
